//
//  BGParam.m
//  bigMall
//
//  Created by LTOVE on 16/7/13.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGParam.h"
#import "NSString+Hash.h"
@implementation BGParam

- (NSString *)sendTime
{
    
    NSTimeInterval a = [ [NSDate date] timeIntervalSince1970] *1000;
    NSString *time = [NSString stringWithFormat:@"%f",a];
    NSString *timer = [time substringToIndex:10];
    return timer;
}


- (NSString *)encryption
{
    return @"off";
}

- (NSString *)expiry
{
    return @"0";
}

- (NSString *)ver
{
    return @"ver1";
}


- (void)setTextDataDic:(NSDictionary *)textDataDic
{
    _textDataDic = textDataDic;
    self.textData = [self dicToJsonStringWithDic:textDataDic];
    //    self.textDataDic = nil;
}

- (NSString *)appkey
{
    return [self signPublic:self.sendTime ver:self.ver encryption:self.encryption module:self.module dir:self.dir controller:self.controller action:self.action];
}

/**
 *  字典转json
 *
 *  @param dict 要转换的字典
 *
 *  @return 转换后的json字符串
 */
- (NSString *)dicToJsonStringWithDic:(NSDictionary *)dict
{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization
                        dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString;
    if ([jsonData length] > 0 && error == nil){
//        GWLog(@"Successfully serialized the dictionary into data.");
        //NSData转换为String
        jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
        jsonString = [jsonString stringByReplacingOccurrencesOfString:@" " withString:@""];
        //            GWLog(@"JSON String = %@", jsonString);
    }
    else if ([jsonData length] == 0 &&
             error == nil){
        //        GWLog(@"No data was returned after serialization.");
        jsonString = @" ";
    }
    else if (error != nil){
        //        GWLog(@"An error happened = %@", error);
        jsonString = @" ";
    }
    return jsonString;
}
/**
 *  签名方法
 *
 *  @return 加密后的MD5字符串
 */
- (NSString *)signPublic:(NSString *)sendTime ver:(NSString *)ver encryption:(NSString *)encryption module:(NSString *)module dir:(NSString *)dir controller:(NSString *)controller action:(NSString *)action
{
    NSString *keyA = [sendTime substringWithRange:NSMakeRange(0, 5)];
    NSString *keyB = [sendTime substringWithRange:NSMakeRange(5, 5)];
    NSString *str = [NSString stringWithFormat:@"%@%@%@%@%@%@%@%@",ver,keyA,encryption,module,dir,keyB,controller,action];
    
    return str.md5String;
}

+ (BGParam *)getParamWithaction:(NSString *)action dictionary :(NSDictionary *)dictionary
{
    BGParam * param = [[BGParam alloc]init];
    param.textData = [param dicToJsonStringWithDic:dictionary];
    
    [param setParamesWithAction:action param:param];
    return param;
}

- (BGParam *)setParamesWithAction:(NSString *)action param:(BGParam *)param
{
    
    if ([action isEqualToString:@"login"]) {
        param.module = @"user";
        param.dir = @"user";
        param.controller = @"ApiUser";
        param.action = @"login";
    }else if ([action isEqualToString:@"sendSmsCode"]){
        param.module = @"user";
        param.dir = @"auth";
        param.controller = @"ApiAuth";
        param.action = @"sendSmsCode";
    }else if ([action isEqualToString:@"sendSmsCodeNoPic"]){
        param.module = @"user";
        param.dir = @"user";
        param.controller = @"ApiUser";
        param.action = @"sendSmsCode";
    }else if ([action isEqualToString:@"userRegister"]){
        param.module = @"user";
        param.dir = @"user";
        param.controller = @"ApiUser";
        param.action = @"userRegister";
    }else if ([action isEqualToString:@"logout"]){
        param.module = @"user";
        param.dir = @"user";
        param.controller = @"ApiUser";
        param.action = @"logout";
    }else if ([action isEqualToString:@"userInfo"]){
        param.module = @"user";
        param.dir = @"user";
        param.controller = @"ApiUser";
        param.action = @"userInfo";
    }else if ([action isEqualToString:@"forgetLoginPwd"]){
        param.module = @"user";
        param.dir = @"auth";
        param.controller = @"ApiAuth";
        param.action = @"forgetLoginPwd";
    }else if ([action isEqualToString:@"forgetPayPwd"]){
        param.module = @"user";
        param.dir = @"auth";
        param.controller = @"ApiAuth";
        param.action = @"forgetPayPwd";
    }else if ([action isEqualToString:@"updateLoginPwd"]){
        param.module = @"user";
        param.dir = @"auth";
        param.controller = @"ApiAuth";
        param.action = @"updateLoginPwd";
    }else if ([action isEqualToString:@"updatePayPwd"]){
        param.module = @"user";
        param.dir = @"auth";
        param.controller = @"ApiAuth";
        param.action = @"updateLoginPwd";
    }else if ([action isEqualToString:@"checkCode"]){
        param.module = @"tools";
        param.dir = @"tools";
        param.controller = @"ApiVerify";
        param.action = @"checkCode";
    }else if ([action isEqualToString:@"updatePhone"]){
        param.module = @"user";
        param.dir = @"auth";
        param.controller = @"ApiAuth";
        param.action = @"updatePhone";
    }else if ([action isEqualToString:@"goodsList"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiMall";
        param.action = @"goodsList";
    }else if ([action isEqualToString:@"goodsCategory"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiMall";
        param.action = @"goodsCategory";
    }else if ([action isEqualToString:@"areaList"]){
        param.module = @"user";
        param.dir = @"tools";
        param.controller = @"ApiArea";
        param.action = @"areaList";
    }else if ([action isEqualToString:@"goodsDetail"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiMall";
        param.action = @"goodsDetail";
    }else if ([action isEqualToString:@"calculateMargin"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiMargin";
        param.action = @"calculateMargin";
    }else if ([action isEqualToString:@"calculateCredit"]){
        param.module = @"user";
        param.dir = @"user";
        param.controller = @"ApiCredit";
        param.action = @"calculateCredit";
    }else if ([action isEqualToString:@"addStepPrices"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiGoods";
        param.action = @"addStepPrices";
    }else if ([action isEqualToString:@"getStepPrice"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiGoods";
        param.action = @"getStepPrice";
    }else if ([action isEqualToString:@"getAddGoodsParam"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiGoods";
        param.action = @"getAddGoodsParam";
    }else if ([action isEqualToString:@"addGoodsStepOne"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiGoods";
        param.action = @"addGoodsStepOne";
    }else if ([action isEqualToString:@"attrChangePrice"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiMall";
        param.action = @"attrChangePrice";
    }else if ([action isEqualToString:@"addCollect"]){
        param.module = @"user";
        param.dir = @"user";
        param.controller = @"ApiCollect";
        param.action = @"addCollect";
    }else if ([action isEqualToString:@"checkCollect"]){
        param.module = @"user";
        param.dir = @"user";
        param.controller = @"ApiCollect";
        param.action = @"checkCollect";
    }else if ([action isEqualToString:@"cancelCollect"]){
        param.module = @"user";
        param.dir = @"user";
        param.controller = @"ApiCollect";
        param.action = @"cancelCollect";
    }else if ([action isEqualToString:@"ownInviteList"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiInvite";
        param.action = @"ownInviteList";
    }else if ([action isEqualToString:@"ownInviteDown"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiInvite";
        param.action = @"ownInviteDown";
    }else if ([action isEqualToString:@"ownInviteBidLogs"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiInvite";
        param.action = @"ownInviteBidLogs";
    }else if ([action isEqualToString:@"categoryAttr"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiMall";
        param.action = @"categoryAttr";
    }else if ([action isEqualToString:@"replyBid"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiInvite";
        param.action = @"replyBid";
    }else if ([action isEqualToString:@"addInviteOne"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiInvite";
        param.action = @"addInviteOne";
    }else if ([action isEqualToString:@"addInviteTwo"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiInvite";
        param.action = @"addInviteTwo";
    }else if ([action isEqualToString:@"ownFundList"]){
        param.module = @"user";
        param.dir = @"user";
        param.controller = @"ApiFund";
        param.action = @"ownFundList";
    }else if ([action isEqualToString:@"ownFund"]){
        param.module = @"user";
        param.dir = @"user";
        param.controller = @"ApiFund";
        param.action = @"ownFund";
    }else if ([action isEqualToString:@"ownCreditList"]){
        param.module = @"user";
        param.dir = @"user";
        param.controller = @"ApiCredit";
        param.action = @"ownCreditList";
    }else if ([action isEqualToString:@"ownCredit"]){
        param.module = @"user";
        param.dir = @"user";
        param.controller = @"ApiCredit";
        param.action = @"ownCredit";
    }else if ([action isEqualToString:@"updateCredit"]){
        param.module = @"user";
        param.dir = @"user";
        param.controller = @"ApiCredit";
        param.action = @"updateCredit";
    }else if ([action isEqualToString:@"ownCollectList"]){
        param.module = @"user";
        param.dir = @"user";
        param.controller = @"ApiCollect";
        param.action = @"ownCollectList";
    }else if ([action isEqualToString:@"ownMessageList"]){
        param.module = @"user";
        param.dir = @"user";
        param.controller = @"ApiMessage";
        param.action = @"ownMessageList";
    }else if ([action isEqualToString:@"sendMessage"]){
        param.module = @"user";
        param.dir = @"user";
        param.controller = @"ApiMessage";
        param.action = @"sendMessage";
    }else if ([action isEqualToString:@"deleteMessage"]){
        param.module = @"user";
        param.dir = @"user";
        param.controller = @"ApiMessage";
        param.action = @"deleteMessage";
    }else if ([action isEqualToString:@"ownSadList"]){
        param.module = @"user";
        param.dir = @"user";
        param.controller = @"ApiSad";
        param.action = @"ownSadList";
    }else if ([action isEqualToString:@"deleteOwnSad"]){
        param.module = @"user";
        param.dir = @"user";
        param.controller = @"ApiSad";
        param.action = @"deleteOwnSad";
    }else if ([action isEqualToString:@"ownBidLog"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiBid";
        param.action = @"ownBidLog";
    }else if ([action isEqualToString:@"cancelBid"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiBid";
        param.action = @"cancelBid";
    }else if ([action isEqualToString:@"ownGoodsList"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiGoods";
        param.action = @"ownGoodsList";
    }else if ([action isEqualToString:@"ownGoodsDetail"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiGoods";
        param.action = @"ownGoodsDetail";
    }else if ([action isEqualToString:@"ownGoodsDown"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiGoods";
        param.action = @"ownGoodsDown";
    }else if ([action isEqualToString:@"ownGoodsDel"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiGoods";
        param.action = @"ownGoodsDel";
    }else if ([action isEqualToString:@"addGoodsStepTwo"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiGoods";
        param.action = @"addGoodsStepTwo";
    }else if ([action isEqualToString:@"addGoodsStepThree"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiGoods";
        param.action = @"addGoodsStepThree";
    }else if ([action isEqualToString:@"sellerOrderList"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiSellerOrder";
        param.action = @"sellerOrderList";
    }else if ([action isEqualToString:@"sellerOrderDetail"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiSellerOrder";
        param.action = @"sellerOrderDetail";
    }else if ([action isEqualToString:@"buyerOrderList"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiBuyerOrder";
        param.action = @"buyerOrderList";
    }else if ([action isEqualToString:@"buyerOrderDetail"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiBuyerOrder";
        param.action = @"buyerOrderDetail";
    }else if ([action isEqualToString:@"submitAuthInfo"]){
        param.module = @"user";
        param.dir = @"user";
        param.controller = @"ApiUser";
        param.action = @"submitAuthInfo";
    }else if ([action isEqualToString:@"sellerOrderStep"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiSellerOrder";
        param.action = @"sellerOrderStep";
    }else if ([action isEqualToString:@"checkPayPasswordCashCredit"]){
        param.module = @"user";
        param.dir = @"auth";
        param.controller = @"ApiAuth";
        param.action = @"checkPayPasswordCashCredit";
    }else if ([action isEqualToString:@"buyerOrderStep"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiBuyerOrder";
        param.action = @"buyerOrderStep";
    }else if ([action isEqualToString:@"getImageInfo"]){
        param.module = @"tools";
        param.dir = @"tools";
        param.controller = @"ApiImage";
        param.action = @"getImageInfo";
    }else if ([action isEqualToString:@"checkCode"]){
        param.module = @"tools";
        param.dir = @"tools";
        param.controller = @"ApiVerify";
        param.action = @"checkCode";
    }else if ([action isEqualToString:@"ownAppraiseList"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiAppraise";
        param.action = @"ownAppraiseList";
    }else if ([action isEqualToString:@"ownAuction"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiAuction";
        param.action = @"ownAuction";
    }else if ([action isEqualToString:@"inviteList"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiInvite";
        param.action = @"inviteList";
    }else if ([action isEqualToString:@"inviteDetail"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiInvite";
        param.action = @"inviteDetail";
    }else if ([action isEqualToString:@"auctionList"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiAuction";
        param.action = @"auctionList";
    }else if ([action isEqualToString:@"auctionDetail"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiAuction";
        param.action = @"auctionDetail";
    }else if ([action isEqualToString:@"updateHeadPhoto"]){
        param.module = @"user";
        param.dir = @"auth";
        param.controller = @"ApiAuth";
        param.action = @"updateHeadPhoto";
    }else if ([action isEqualToString:@"beforeAuction"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiAuction";
        param.action = @"beforeAuction";
    }else if ([action isEqualToString:@"addAuctionLog"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiAuction";
        param.action = @"addAuctionLog";
    }else if ([action isEqualToString:@"createOrder"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiMall";
        param.action = @"createOrder";
    }else if ([action isEqualToString:@"sellerGoBid"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiInvite";
        param.action = @"sellerGoBid";
    }else if ([action isEqualToString:@"auctionLogList"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiAuction";
        param.action = @"auctionLogList";
    }else if ([action isEqualToString:@"bidList"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiBid";
        param.action = @"bidList";
    }else if ([action isEqualToString:@"addSad"]){
        param.module = @"user";
        param.dir = @"user";
        param.controller = @"ApiSad";
        param.action = @"addSad";
    }else if ([action isEqualToString:@"sadList"]){
        param.module = @"user";
        param.dir = @"user";
        param.controller = @"ApiSad";
        param.action = @"sadList";
    }else if ([action isEqualToString:@"returnMargins"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiGoods";
        param.action = @"returnMargin";
    }else if ([action isEqualToString:@"returnMarginb"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiInvite";
        param.action = @"returnMargin";
    }else if ([action isEqualToString:@"ownInviteDel"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiInvite";
        param.action = @"ownInviteDel";
    }else if ([action isEqualToString:@"ownInviteDetails"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiInvite";
        param.action = @"ownInviteDetail";
    }else if ([action isEqualToString:@"noticeList"]){
        param.module = @"article";
        param.dir = @"notice";
        param.controller = @"ApiNotice";
        param.action = @"noticeList";
    }else if ([action isEqualToString:@"imagesList"]){
        param.module = @"user";
        param.dir = @"tools";
        param.controller = @"ApiImages";
        param.action = @"imagesList";
    }else if ([action isEqualToString:@"readAddGoodsStepTwo"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiGoods";
        param.action = @"readAddGoodsStepTwo";
    }else if ([action isEqualToString:@"readAddGoodsStepThree"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiGoods";
        param.action = @"readAddGoodsStepThree";
    }else if ([action isEqualToString:@"getInfoByNamePhone"]){
        param.module = @"user";
        param.dir = @"user";
        param.controller = @"ApiUser";
        param.action = @"getInfoByNamePhone";
    }else if ([action isEqualToString:@"articleList"]){
        param.module = @"article";
        param.dir = @"article";
        param.controller = @"ApiArticle";
        param.action = @"articleList";
    }else if ([action isEqualToString:@"brandList"]){
        param.module = @"trade";
        param.dir = @"mall";
        param.controller = @"ApiMall";
        param.action = @"brandList";
    }
    
    return param;
}

@end
